using System;
using TMPro;
using Unity.Mathematics;
using UnityEngine;

namespace GpuHud
{
    [Serializable]
    public class GpuHudTextStyleAsset : GpuHudStyleBase<TextStyleShaderData>
    {
        [SerializeField]
        public float3 Offset;
        [SerializeField]
        public float OriginSize;
        [SerializeField]
        public GpuFlyTextAnim flyAnimType;
        //序列化数据
        [SerializeField] 
        private float m_CharacterSpacing;
        public float CharacterSpacing {
            get
            {
                return m_CharacterSpacing;
            }
        }
        [SerializeField]
        private string[] m_Texts;
        [SerializeField]
        private TMP_FontAsset[] m_FontAssets;
        // Asset资源，脚本仅限读取
        public string Text => m_Texts[m_StateIdx];

        [SerializeField] private bool isLocalization;

        public bool IsLocalization
        {
            get { return isLocalization; }
        }
        [SerializeField]
        private bool isDamageTmpLocalization;

        private string currentFontName = "";
        private TMP_FontAsset _localizationFontAsset = null;

        public void SetColor(Color color)
        {
            m_ShaderOriginData[m_StateIdx].FaceColor = color;
        }
        public TMP_FontAsset FontAsset
        {
            get
            {
                if (isLocalization)
                {
                    if (isDamageTmpLocalization)
                    {
                        if (currentFontName != GpuHudFacade.GpuFacade.DamageTmpAssetName || _localizationFontAsset == null)
                        {
                            GpuHudFacade.localizationProvider?.Invoke(
                                GpuHudFacade.GpuFacade.DamageTmpAssetName,
                                (font) =>
                                {
                                    if (font)
                                    {
                                        currentFontName = GpuHudFacade.GpuFacade.DamageTmpAssetName;
                                        _localizationFontAsset = font;
                                    }
                                });
                        }
                    }
                    else
                    {
                        if (currentFontName != GpuHudFacade.GpuFacade.NormalTmpAssetName || _localizationFontAsset == null)
                        {
                            GpuHudFacade.localizationProvider?.Invoke(
                                GpuHudFacade.GpuFacade.NormalTmpAssetName,
                                (font) =>
                                {
                                    if (font)
                                    {
                                        currentFontName = GpuHudFacade.GpuFacade.NormalTmpAssetName;
                                        _localizationFontAsset = font;
                                    }
                                });
                        }
                    }

                    if (_localizationFontAsset != null)
                    {
                        return _localizationFontAsset;
                    }
                    else
                    {
                        return GpuHudFacade.GpuFacade.Settings.DefaultFontAsset;
                    }
                }

                return m_FontAssets[m_StateIdx];
            }
        }

        [SerializeField]
        public float ItalicOffset;

        public override int StateCount
        {
            get => base.StateCount;
            set
            {
                base.StateCount = value;
                Array.Resize(ref m_Texts, value);
                Array.Resize(ref m_FontAssets, value);
            }
        }

        public override void RemoveStateAt(int idx)
        {
            base.RemoveStateAt(idx);
            RemoveAt(ref m_Texts, idx);
        }
    }
}
